package com.ruoyi.web.controller.pk;

import java.util.*;
import java.util.stream.Collectors;

import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.pk.domain.PkEjDailyReport;
import com.ruoyi.pk.res.PkZlDailyReportTjExport;
import com.ruoyi.system.mapper.SysDeptMapper;
import com.ruoyi.system.mapper.SysUserMapper;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.pk.domain.PkZlDailyReport;
import com.ruoyi.pk.service.IPkZlDailyReportService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 主粮日报Controller
 *
 * @author wang
 * @date 2021-09-10
 */
@RestController
@RequestMapping("/pk/zl_daily_report")
public class PkZlDailyReportController extends BaseController
{
    @Autowired
    private IPkZlDailyReportService pkZlDailyReportService;

    @Autowired
    private SysUserMapper userMapper;

    /**
     * 查询主粮日报列表
     *
     * 注：用户绑定了问题等级（A,B），查看对应级别的数据，如果为空，则查看全部数据
     *
     * A类问题同步上传至总经理、主任和徐总、白总
     * B类问题同步上传至总经理、主任和徐总
     * C类问题同步上传至生产主任和生产主管
     */
    @PreAuthorize("@ss.hasPermi('pk:zl_daily_report:list')")
    @GetMapping("/list")
    public TableDataInfo list(PkZlDailyReport pkZlDailyReport)
    {
        startPage();
        SysUser user = userMapper.selectUserById(SecurityUtils.getLoginUser().getUser().getUserId());
        String zlDj = user.getZlDj();//问题等级
        String bz = user.getRemark();//管辖区域
        pkZlDailyReport.setWtdj(zlDj);//用来mybatis判断该字段是否为空，以便知道是否执行对应的条件
        pkZlDailyReport.setWtdjList(zlDj.split("、"));//问题等级条件
        pkZlDailyReport.setBz(bz);//用来mybatis判断该字段是否为空，以便知道是否执行对应的条件
        pkZlDailyReport.setBzList(bz.split("、"));//管辖区域条件
        List<PkZlDailyReport> pkZlDailyReportList = pkZlDailyReportService.selectPkZlDailyReportList(pkZlDailyReport);
        List<PkZlDailyReport> list = new ArrayList<>(pkZlDailyReportList);

        //根据角色判断可以查看哪些流程节点信息，流程（不可越级）：品控班长->车间主任->品控经理
        String roleKey = SecurityUtils.getLoginUser().getUser().getRoles().get(0).getRoleKey();//查询当前登录者的角色信息
        /*
        *********暂时未使用*********
        if(roleKey.equals("zl_zrr")){ //主粮责任人
            list = list.stream().filter(item ->  item.getSftj().equals("1")).collect(Collectors.toList());
        *********暂时未使用*********
        }*/
        if(roleKey.equals("zl_pkbz")){ //主粮品控班长
            list = list.stream().filter(item ->
                    !("").equals(item.getYyfx()) && null != item.getYyfx() &&
                    !("").equals(item.getJzcs()) && null != item.getJzcs() &&
                    !("").equals(item.getGzyz()) && null != item.getGzyz() &&
                    item.getSftj().equals("1") || item.getSftj().equals("2")).collect(Collectors.toList());
        }
        if(roleKey.equals("zl_zjzr")){ //主粮车间主任
            list = list.stream().filter(item -> !("").equals(item.getPkbzyj()) && null != item.getPkbzyj() && item.getSftj().equals("1") || item.getSftj().equals("2")).collect(Collectors.toList());
        }
        if(roleKey.equals("zl_pkjl")){ //主粮品控经理
            list = list.stream().filter(item -> !("").equals(item.getZryj()) && null != item.getZryj() && item.getSftj().equals("1") || item.getSftj().equals("2")).collect(Collectors.toList());
        }

        return getDataTable(list);
    }

    /**
     * 导出主粮日报列表
     */
    @PreAuthorize("@ss.hasPermi('pk:zl_daily_report:export')")
    @Log(title = "主粮日报", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(PkZlDailyReport pkZlDailyReport)
    {
        List<PkZlDailyReport> list = pkZlDailyReportService.selectPkZlDailyReportList(pkZlDailyReport);
        ExcelUtil<PkZlDailyReport> util = new ExcelUtil<PkZlDailyReport>(PkZlDailyReport.class);
        return util.exportExcel(list, "主粮日报数据");
    }

    /**
     * 获取主粮日报详细信息
     */
    @PreAuthorize("@ss.hasPermi('pk:zl_daily_report:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(pkZlDailyReportService.selectPkZlDailyReportById(id));
    }

    /**
     * 新增主粮日报
     */
    @PreAuthorize("@ss.hasPermi('pk:zl_daily_report:add')")
    @Log(title = "主粮日报", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PkZlDailyReport pkZlDailyReport)
    {
        return toAjax(pkZlDailyReportService.insertPkZlDailyReport(pkZlDailyReport));
    }

    /**
     * 修改主粮日报
     */
    //@PreAuthorize("@ss.hasPermi('pk:zl_daily_report:edit')")
    @Log(title = "主粮日报", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PkZlDailyReport pkZlDailyReport)
    {
        return toAjax(pkZlDailyReportService.updatePkZlDailyReport(pkZlDailyReport));
    }

    /**
     * 删除主粮日报
     */
    @PreAuthorize("@ss.hasPermi('pk:zl_daily_report:remove')")
    @Log(title = "主粮日报", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(pkZlDailyReportService.deletePkZlDailyReportByIds(ids));
    }

    /**
     * 班组问题数量统计
     */
    @GetMapping("/getListByBzSl")
    public TableDataInfo getListByBzSl(PkZlDailyReport pkZlDailyReport) {
        List<PkZlDailyReport> list = pkZlDailyReportService.getListByBzSl(pkZlDailyReport);
        return getDataTable(list);
    }

    /**
     * 各问题类别数量
     */
    @GetMapping("/getListByWtlb")
    public TableDataInfo getListByWtlb(PkZlDailyReport pkZlDailyReport) {
        List<PkZlDailyReport> list = pkZlDailyReportService.getListByWtlb(pkZlDailyReport);
        return getDataTable(list);
    }

    /**
     * 各班组问题等级数量统计
     */
    @GetMapping("/getListByBzWtdj")
    public TableDataInfo getListByBzWtdj(PkZlDailyReport pkZlDailyReport) {

        List<PkZlDailyReport> list = pkZlDailyReportService.getListByBzWtdj(pkZlDailyReport);
        return getDataTable(list);
    }

    /**
     * 周报
     * 班组问题数量统计
     */
    @GetMapping("/getListByBzSl_Z")
    public TableDataInfo getListByBzSl_Z(PkZlDailyReport pkZlDailyReport) {
        List<PkZlDailyReport> list = pkZlDailyReportService.getListByBzSl_Z(pkZlDailyReport);
        return getDataTable(list);
    }

    /**
     * 周报
     * 各问题类别数量
     */
    @GetMapping("/getListByWtlb_Z")
    public TableDataInfo getListByWtlb_Z(PkZlDailyReport pkZlDailyReport) {
        List<PkZlDailyReport> list = pkZlDailyReportService.getListByWtlb_Z(pkZlDailyReport);
        return getDataTable(list);
    }

    /**
     * 周报
     * 各班组问题等级数量统计
     */
    @GetMapping("/getListByBzWtdj_Z")
    public TableDataInfo getListByBzWtdj_Z(PkZlDailyReport pkZlDailyReport) {
        List<PkZlDailyReport> list = pkZlDailyReportService.getListByBzWtdj_Z(pkZlDailyReport);
        return getDataTable(list);
    }

    /**
     * 查询主粮已处理完毕、未处理数量
     */
    @GetMapping("/getZlSl")
    public Map<String,Long> getZlSl(PkZlDailyReport pkZlDailyReport) {
        Long ycl = pkZlDailyReportService.selectPkZlDailyReportList(pkZlDailyReport).stream().filter(item -> item.getSftj().equals("2")).count();
        Long dcl = pkZlDailyReportService.selectPkZlDailyReportList(pkZlDailyReport).stream().filter(item -> !item.getSftj().equals("2")).count();
        Map<String,Long> map = new HashMap<>();
        map.put("zl_ycl",ycl);
        map.put("zl_dcl",dcl);
        return map;
    }
}
